package kr.saeildamil.project.secretary.fragment;

import kr.saeildamil.project.secretary.CurrentTime;
import kr.saeildamil.project.secretary.Main;
import kr.saeildamil.project.secretary.R;
import kr.saeildamil.project.secretary.DB.DBHelper;
import android.app.Dialog;
import android.app.DialogFragment;
import android.app.TimePickerDialog;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.TimePicker;
import android.widget.Toast;

public class UpdateSchedult extends DialogFragment {
	static TextView time;
	static Button complete;
	static int pickHour;
	static int pickMinute;
	static String hourString;
	static String minString;
	static DBHelper dbHelper;
	static SQLiteDatabase db;
	static String date;
	static String task;
	static EditText detail;
	static String todayDetail;
	static Spinner spin;
	static ArrayAdapter<CharSequence> adspin;
	static String TAG="UpdateSchedult";
	static int dbId;
	CurrentTime currenttime;
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View root=inflater.inflate(R.layout.updateschedule, container, false);
		
		time=(TextView) root.findViewById(R.id.update_time);
		spin=(Spinner) root.findViewById(R.id.update_spinner);
		detail=(EditText) root.findViewById(R.id.update_detail);
		
		//선택항목 그대로 출력
		Main main=new Main();
		dbId=main.getDbId();
		Log.d(TAG, String.valueOf(dbId));
		
		dbHelper=new DBHelper(getActivity());
		db=dbHelper.getReadableDatabase();
		Cursor cursor;
		cursor=db.rawQuery("SELECT * FROM schedule WHERE _id='"+dbId+"';", null);
		while (cursor.moveToNext()) {
			//Log.d(TAG, cursor.getString(2)+cursor.getShort(3));
			time.setText(cursor.getString(2)+":"+cursor.getString(3));
			spin.setPrompt(cursor.getString(4));
			detail.setText(cursor.getString(5));
		}
		dbHelper.close();
		cursor.close();
		
		adspin=ArrayAdapter.createFromResource(getActivity(), R.array.task, android.R.layout.simple_spinner_item);
		adspin.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		spin.setAdapter(adspin);
		spin.setOnItemSelectedListener(new OnItemSelectedListener() {
			@Override
			public void onItemSelected(AdapterView<?> arg0, View arg1,
					int position, long arg3) {
				task=adspin.getItem(position).toString();
			}
			@Override
			public void onNothingSelected(AdapterView<?> arg0) {
				// TODO Auto-generated method stub
			}
		});
		
		//timepickerdialog 
		
		time.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Toast.makeText(getActivity(), "timepicker호출", 0).show();
				new TimePickerDialog(getActivity(), mTimeSetListener, pickHour, pickMinute, false).show();
			}
		});
		
		//수정---> DB저장
		Button yes=(Button) root.findViewById(R.id.updateschedule_yes);
		yes.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				//사용자가 시간 선택없을 경우 null 값이 입력되기 때문에 강제로 시간을 입력해준다 
				if(hourString==null){
					hourString=currenttime.currentHour;
					minString=currenttime.currentMinute;
				}
				
				dbHelper=new DBHelper(getActivity());
				db=dbHelper.getWritableDatabase();
				Cursor cursor;
				cursor=db.rawQuery("SELECT * from schedule where _id='"+dbId+"';", null);
				db.execSQL("UPDATE schedule SET hour='"+hourString+"' WHERE _id='"+dbId+"';");
				db.execSQL("UPDATE schedule SET minute='"+minString+"' WHERE _id='"+dbId+"';");
				db.execSQL("UPDATE schedule SET task='"+task+"' WHERE _id='"+dbId+"';");
				db.execSQL("UPDATE schedule SET detail='"+detail.getText()+"' WHERE _id='"+dbId+"';");
				while (cursor.moveToNext()) {
					Log.d(TAG, cursor.getString(0));
					Log.d(TAG, cursor.getString(1));
					Log.d(TAG, cursor.getString(2));
					Log.d(TAG, cursor.getString(3));
					Log.d(TAG, cursor.getString(4));
					Log.d(TAG, cursor.getString(5));
				}
				Toast.makeText(getActivity(), "DB 수정 완료" , 0).show();
				dbHelper.close();
				cursor.close();
				
				Intent intent=new Intent(getActivity(), Main.class);
				intent=intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
				startActivity(intent);
			}
		});
		return root;
	}

	TimePickerDialog.OnTimeSetListener mTimeSetListener=new TimePickerDialog.OnTimeSetListener() {
		@Override
		public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
			pickHour=hourOfDay;
			pickMinute=minute;
			hourString=String.valueOf(hourOfDay);
			minString=String.valueOf(minute);

			if(hourString.length()==1){
				hourString="0"+hourString;
			}
			if(minString.length()==1){
				minString="0"+minString;
			}

			time.setText(hourString+":"+minString);
		}
	};
	
	@Override
	public Dialog onCreateDialog(Bundle savedInstanceState) {
		Dialog dialog=new Dialog(getActivity());
		return dialog;
	}
}
